package com.android.zmusic.service;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;

import com.android.zmusic.interfaces.IOnServiceConnectComplete;
import com.android.zmusic.myaidl.IMusicConnect;

public class ServiceManager {
	
	private final static String SERVICE_NAME = "com.android.zmusic.service.musicservices";
	
	private Boolean mConnectComplete ;
	
	private ServiceConnection mServiceConnection ;
	
	private IMusicConnect mMusicConnect ;
	
	private IOnServiceConnectComplete  mInServiceConnectComplete;
	
	private Context context ;
	
	
	/**
	 */

	public ServiceManager(Context context) {
		this.context = context ;
		init();
	}

	private void init() {

		mServiceConnection = new ServiceConnection() {
			
			@Override
			public void onServiceDisconnected(ComponentName name) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onServiceConnected(ComponentName name, IBinder service) {
				
				mMusicConnect = IMusicConnect.Stub.asInterface(service);
				if(mMusicConnect != null) {
					if(null != mInServiceConnectComplete) {
						
						mInServiceConnectComplete.OnServiceConnectComplete();
					}
				}
			}
		};
		
		mConnectComplete = false;
		mMusicConnect = null;
	}
	
	/**
	 * 连接服务
	 * @return 
	 */
	public boolean connectService() {
	
		if(mConnectComplete) {
			return true;
		}
		
		Intent intent = new Intent(SERVICE_NAME) ;
		if(null !=context) {
			context.bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
			mConnectComplete = true;
			return true;
		}
		
		return false;
	}
	
	/**
	 * 断开连接
	 * @return
	 */
	public boolean disConnectService() {
		
		if(!mConnectComplete) {
			return false;
		}
		
		Intent intent = new Intent(SERVICE_NAME);
		if(null != context ) {
			context.unbindService(mServiceConnection);
			mMusicConnect = null;
			mConnectComplete = false;
			return true;
		}
		return false;
	}

	
	public Object getCurPosition() {
		// TODO Auto-generated method stub
		return null;
	}

	public void setOnServiceConnectComplete(
			Context context) {
		// TODO Auto-generated method stub
		
	}



	public void seekTo(int progress) {
		// TODO Auto-generated method stub
		
	}

}
